<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;


class Admin extends Backend
{
    //覆盖全局属性 不需要登录
    protected $NoNeedLogin = ['login', 'captcha', 'logout'];

    public function __construct(){

        //继承父类
        parent::__construct();
        //创建公共的属性
        $this->AdminModel = model('Admin');
    }

    
    // 
    public function index()
    {
        $count = $this->AdminModel->count();

        $countlist = $this->AdminModel->order('job', 'desc')->paginate(10);

        $pagestr = $countlist->render();

        // var_dump($countlist);
        // exit;

        $this->assign([
            'count' => $count,
            'countlist' => $countlist,
            'pagestr' => $pagestr
        ]);

        return $this->fetch();
    }

    public function del()
    {
        if($this->request->isPost()){

            $success = [
                'result' => false,
                'msg' => ''
            ];

            $ids = $this->request->param('ids', 0);

            $result = $this->AdminModel->destroy($ids);

            if($result === FALSE)
            {
                $success['result'] = false;
                $success['msg'] = $this->AdminModel->getError();
            }else
            {
                $success['result'] = true;
                $success['msg'] = '删除成功';
            }

            return json($success);
        }
    }

    // 管理员添加
    public function add()
    {

        if ($this->request->isPost()) {
            $param = $this->request->param();
            // 查找管理员
            $adminboolen = $this->AdminModel->where('username', $param['username'])->find();
            if ($adminboolen) {
                $this->error('管理员已存在');
                exit;
            }
            $salt = randstr(10);
            $where = [
                'username' => $param['username'],
                'nickname' => $param['nickname'],
                'password' => md5($param['password'] . $salt),
                'job' => $param['job'],
                'salt' => $salt
            ];
            $res = $this->AdminModel->save($where);
            if (!$res) {
                $this->error('添加管理员失败');
                exit;
            }
            $this->success('添加管理成功', '/admin/admin/index');
        }

        return $this->fetch();
    }

    // 编辑管理员
    public function edit()
    {
        // 登录管理员id
        $ownid = session('LoginAdmin');

        
        $owninfo = $this->AdminModel->find($ownid['id']);

        
        // 获取编辑ID
        $editid = $this->request->param('id', '');
        // 判断管理员是否存在
        $editadmininfor = $this->AdminModel->where('id', $editid)->find();

        
        
        if (!$editadmininfor) {
            $this->error('管理员不存在', '/admin/admin/index');
            exit;
        }
        // 判读是否自己的账号
        if($editid != $ownid['id'] && $owninfo['job'] != 1){
            $this->error('你没有最高权限不能修改他人资料');
            exit;
        }
        $this->assign([
            'username' => $editadmininfor['username'],
            'nickname' => $editadmininfor['nickname'],
            'job' => $editadmininfor['job']
        ]);
        if($this->request->isPost()){
            $param = $this->request->param();
            $rspss = $this->request->param('rspss','');
            
        
            $data = [
                'nickname' => $param['nickname'],
                
            ];


            if($rspss){
                if(md5($param['rspss'].$editadmininfor['salt']) != $editadmininfor['password']){
                    $this->error('密码错误');
                    exit;
                }
                if($param['password'] == $param['rspss']){
                    $this->error('更改的密码不能与原密码相同');
                    exit;
                }
                $data['password'] = md5($param['password'].$editadmininfor['salt']);
            }
            $countdiction = $this->AdminModel->withTrashed()->where('job',2)->count();
            if($countdiction >= 2){
                $this->error('编辑失败，最高权限只能有2个');
                exit;
            }
            $result = $this->AdminModel->where('id', $editid)->update($data);
            if(!$result){
                $this->error('编辑失败');
                exit;
            }else{
                $this->success('编辑成功','/admin/admin/index');
                exit;
            }
        }
        return $this->fetch();
    }
}
